home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch09
/
fig09_08.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
2KB
|
63 lines
1 // Fig. 9.8: point2.h
2 // Definition of class Point
3 #ifndef POINT2_H
4 #define POINT2_H
5
6 class Point {
7 friend ostream &operator<<( ostream &, const Point & );
8 public:
9 Point( int = 0, int = 0 ); // default constructor
10 void setPoint( int, int ); // set coordinates
11 int getX() const { return x; } // get x coordinate
12 int getY() const { return y; } // get y coordinate
13 protected: // accessible to derived classes
14 int x, y; // coordinates of the point
15 };
16
17 #endif
18
19
20 // Fig. 9.8: point2.cpp
21 // Member functions for class Point
22 #include <iostream.h>
23 #include "point2.h"
24
25 // Constructor for class Point
26 Point::Point( int a, int b ) { setPoint( a, b ); }
27
28 // Set the x and y coordinates
29 void Point::setPoint( int a, int b )
30 {
31 x = a;
32 y = b;
33 }
34
35 // Output the Point
36 ostream &operator<<( ostream &output, const Point &p )
37 {
38 output << '[' << p.x << ", " << p.y << ']';
39
40 return output; // enables cascading
41 }
42
43
44 // Fig. 9.8: fig09_08.cpp
45 // Driver for class Point
46 #include <iostream.h>
47 #include "point2.h"
48
49 int main()
50 {
51 Point p( 72, 115 ); // instantiate Point object p
52
53 // protected data of Point inaccessible to main
54 cout << "X coordinate is " << p.getX()
55 << "\nY coordinate is " << p.getY();
56
57 p.setPoint( 10, 10 );
58 cout << "\n\nThe new location of p is " << p << endl;
59
60 return 0;
61 }